<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>URLJoin</title>
</head>

<body>
    <div>打开浏览器查看结果</div>
    <script>
        const URLJoin = (...args) =>
            args
                .join('/')
                .replace(/[\/]+/g, '/')
                .replace(/^(.+):\//, '$1://')
                .replace(/^file:/, 'file:/')
                .replace(/\/(\?|&|#[^!])/g, '$1')
                .replace(/\?/g, '&')
                .replace('&', '?');
        console.log(URLJoin('http://www.google.com', 'a', '/b/cd', '?foo=123', '?bar=foo'));
    </script>
</body>

</html>